package cn.dyp.com.logprint.util;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentName;
import android.content.Context;

public class Util {
	/**
	 * 判断当前系统中指定的service是否在运行
	 * 
	 * @param context
	 *            APP上下文
	 * @param className
	 *            service类的名称
	 * @return 如果存在，返回true，否则返回false
	 */
	public static boolean isServiceExisted(Context context, String className) {
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningServiceInfo> serviceList = activityManager
				.getRunningServices(Integer.MAX_VALUE);

		if (serviceList.size() <= 0) {
			return false;
		}

		for (int i = 0; i < serviceList.size(); i++) {
			RunningServiceInfo serviceInfo = serviceList.get(i);
			ComponentName serviceName = serviceInfo.service;

			if (serviceName.getClassName().equals(className)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 快速封装一个soap协议
	 * 
	 * @param bodyContent
	 *            soap协议中的body内容
	 * @return 完整的soap协议
	 */
	public static String getSoapXml(String bodyContent) {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
				+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
				+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+ "<soap:Body>" + bodyContent + "</soap:Body></soap:Envelope>");
		return sb.toString();
	}

	public static String getXmlElementStr(String name, String value) {
		return "<" + name + ">" + value + "</" + name + ">";
	}

	/**
	 * 从一个完整的soap协议内容中解析出soap body内容
	 * 
	 * @param soapXml
	 *            完整的soap协议
	 * @return soap body内容
	 */
	public static Element getSoapBodyElement(String soapXml) {
		Element soapBodyEle = null;

		// 采用doc的方式解析xml内容
		DocumentBuilderFactory factory = null;
		DocumentBuilder builder = null;
		Document document = null;
		StringReader stringReader = new StringReader(soapXml);
		InputSource inputSource = new InputSource(stringReader);

		factory = DocumentBuilderFactory.newInstance();
		try {
			builder = factory.newDocumentBuilder();
			document = builder.parse(inputSource);

			Element root = document.getDocumentElement();
			if (root != null) {
				// 从soap协议中找到soap:Body标签并提取该标签里的内容
				soapBodyEle = (Element) root.getElementsByTagName("soap:Body")
						.item(0);
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return soapBodyEle;
	}

	// 从xml的父标签中提取子标签的内容
	public static String getChildElementValueStr(Element element,
			String childName) {
		Element childEle = (Element) element.getElementsByTagName(childName)
				.item(0);
		if (childEle != null) {
			return childEle.getFirstChild().getNodeValue();
		}
		return "";
	}

	// 从xml的父标签中提取子标签的内容，并把内容转成int类型返回
	public static int getChildElementValueInt(Element element, String childName) {
		int ret = 0;
		String strValue = getChildElementValueStr(element, childName);
		if (strValue.equals("")) {
			try {
				ret = Integer.parseInt(strValue);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ret;
	}
}
